const app = getApp();
const pageSize = 20;
Page({


  data: {
    detailedList: [],
    conditions: {
      start_time: '', //开始时间
      end_time: '', //结束时间
    },
    page: 1,
    drawer: false,
    isFilter: false,
    loadmore: false,
    nomore: false,
    empty: false,
    current: -1,
  },


  onLoad: function (options) {
    this.getPromotionRecord();
  },

  onReachBottom: function () {
    if (this.data.loadmore) {
      this.setData({
        page: this.data.page + 1
      })
      this.getPromotionRecord();
    }
  },

  onPullDownRefresh: function () {
    this.reloadData();
  },

  reloadData() {
    this.setData({
      page: 1,
      detailedList: [],
      nomore: false,
      loadmore: false
    })
    wx.showLoading();
    this.getPromotionRecord();
  },


  change(e) {
    let index = e.detail.index
    this.setData({
      current: this.data.current == index ? -1 : index
    })
  },

  getPromotionRecord: function () {
    app.ajax({
      url: "Partner/Promotion/getPromotionRecordList",
      data: {
        "page": this.data.page,
        "row": pageSize,
        "start_time": this.data.conditions.start_time,
        "end_time": this.data.conditions.end_time
      },
      success: res => {
        wx.hideLoading();
        wx.stopPullDownRefresh();
        if (res.data.code == 1000) {
          const source = res.data.data;
          const list = this.data.detailedList.concat(source.data);
          this.setData({
            detailedList: list,
            empty: source.page.total_number == 0 ? true : false,
            nomore: list.length >= source.page.total_number ? true : false,
            loadmore: list.length < source.page.total_number ? true : false,
            drawer: false
          })
        } else {
          app.showToast(res.data.message);
        }
      },
      error: e => {
        wx.hideLoading();
        wx.stopPullDownRefresh();
        app.showToast("网络错误");
      }
    })
  },


  handleTime(e) {
    const name = e.currentTarget.dataset.name;
    this.setData({
      [`conditions.${name}`]: e.detail.value
    })
  },


  showDrawer() {
    this.condition = {
      ...this.data.conditions
    };
    this.setData({
      drawer: true,
    })
  },

  closeDrawer() {
    this.setData({
      drawer: false,
      conditions: this.condition
    })
    delete(this.condition)
  },

  resetDrawer() {
    const conditions = this.data.conditions;
    for (let x in conditions) {
      conditions[x] = "";
    }
    this.setData({
      conditions,
    })
  },

  confirmDrawer() {
    let isFilter = false;
    const conditions = this.data.conditions;
    for (let x in conditions) {
      conditions[x] !== "" && (isFilter = true);
    }
    this.setData({
      isFilter
    })
    delete(this.condition);
    this.reloadData();
  },

})